package com.cqjtu.checkbook;

import android.content.ContentValues;
import android.content.Context;
import android.content.res.ColorStateList;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import java.util.List;

/**
 * Created by Administrator on 2016/10/29 0029.
 */
public class StudentAdapter extends ArrayAdapter<Student> {
    private int resourceId;
    private MyDBHelper dbHelper;
    private SQLiteDatabase db;
    public StudentAdapter(Context context, int resource, List<Student> objects) {
        super(context, resource, objects);
        resourceId = resource;
        dbHelper = new MyDBHelper(MyApplication.getContext(),"CheckBook.db",null,1);
        db = dbHelper.getWritableDatabase();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Student student = getItem(position);
        View view;
        ViewHolder viewHolder;
        //数据库中state状态
         String s = null;
        if(convertView==null) {
            view = LayoutInflater.from(getContext()).inflate(resourceId, null);
            viewHolder = new ViewHolder();
            viewHolder.textView1=(TextView) view.findViewById(R.id.text1);
            viewHolder.textView2=(TextView) view.findViewById(R.id.text2);
            viewHolder.textView3=(TextView) view.findViewById(R.id.text3);
            viewHolder.textView4=(TextView) view.findViewById(R.id.text4);
            viewHolder.textView5=(TextView) view.findViewById(R.id.text5);
            view.setTag(viewHolder);

        }else{
            view = convertView;
            viewHolder = (ViewHolder) view.getTag();

        }
//        TextView textView1 = (TextView) view.findViewById(R.id.text1);
//        TextView textView2 = (TextView) view.findViewById(R.id.text2);
//        TextView textView3 = (TextView) view.findViewById(R.id.text3);
//        TextView textView4 = (TextView) view.findViewById(R.id.text4);
        viewHolder.textView1.setText("  "+student.getId()+"  ");
        viewHolder.textView2.setText(student.getSno());
        viewHolder.textView3.setText(student.getSname());
        viewHolder.textView4.setText(student.getBanji());

       // Cursor cursor = db.query("CheckBook",null,null,null,null,null,null);

//        if(cursor.moveToFirst()){
//            do{
//                int dt = cursor.getInt(cursor.getColumnIndex("id"));
//                if(dt==student.getId()){
//                    s = cursor.getString(cursor.getColumnIndex("State"));
//                    break;
//                }
//
//            }while(cursor.moveToNext());
//        }

        viewHolder.textView5.setText(student.getState());


        switch (viewHolder.textView5.getText().toString()){
            case "在勤":
                viewHolder.textView5.setTextColor(Color.GREEN);
                break;
            case "迟到":
                viewHolder.textView5.setTextColor(Color.YELLOW);
                break;
            case "早退":
                viewHolder.textView5.setTextColor(Color.RED);
                break;
            case "旷课":
                viewHolder.textView5.setTextColor(Color.RED);
                break;
            case "病假":
                viewHolder.textView5.setTextColor(Color.GRAY);
                break;
        }
//        if(!student.getState().toString().equals(s)){
//            try {
//                Thread.sleep(50);
//                notifyDataSetChanged();
//            } catch (InterruptedException e) {
//                e.printStackTrace();
//            }
//        }
        return  view;
    }
    class ViewHolder{
        TextView textView1;
        TextView textView2;
        TextView textView3;
        TextView textView4;
        TextView textView5;
    }
}
